package cn.itcast.controller.chat;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.ChatLog;
import cn.itcast.domain.cargo.ChatLogExample;
import cn.itcast.domain.User;
import cn.itcast.service.ChatLogService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/chat")
public class ChatController  extends BaseController {

    @Reference
    private ChatLogService chatLogService;

    @RequestMapping("/openChat")
    public String openChat(){
        request.setAttribute("send",getChatLogNum("1"));
        request.setAttribute("receiver",getChatLogNum("2"));
        request.setAttribute("send2",getChatLogNum("3"));
        System.out.println("===========================");
        System.out.println(getChatLogNum("1"));
        System.out.println(getChatLogNum("2"));
        System.out.println(getChatLogNum("3"));
        System.out.println("===========================");
        request.setAttribute("list",getChatLog());
        return "chat";
    }
    @RequestMapping("/qq")
    public String qq(){
        return "redirect://localhost:8089/chatroom";
    }

    @RequestMapping("/save")
    public String save(String message){
        User user =(User)session.getAttribute("loginUser");
        String userName = user.getUserName();
        ChatLog chatLog = new ChatLog();
        chatLog.setSendUser(userName);
        chatLog.setLid(UUID.randomUUID().toString());
        chatLog.setSendContext(message);
        chatLog.setSendTime(new Date());
        chatLogService.save(chatLog);
        request.setAttribute("list",getChatLog());
        request.setAttribute("send",getChatLogNum("1"));
        request.setAttribute("receiver",getChatLogNum("2"));
        request.setAttribute("send2",getChatLogNum("3"));
        System.out.println("===========================");
        System.out.println(getChatLogNum("1"));
        System.out.println(getChatLogNum("2"));
        System.out.println(getChatLogNum("3"));
        System.out.println("===========================");
        return "chat";
    }

    public List<ChatLog> getChatLog(){
        User user =(User)session.getAttribute("loginUser");
        String userName = user.getUserName();
        ChatLogExample chatLogExample = new ChatLogExample();
        chatLogExample.setOrderByClause("send_time asc");
        ChatLogExample.Criteria criteria = chatLogExample.createCriteria();
//        criteria.andSendUserEqualTo(userName);
        List lis = new ArrayList();
        lis.add(userName);
        lis.add("laoli");
        criteria.andSendUserIn(lis);
        List<ChatLog> list = chatLogService.findAll(chatLogExample);
        return list;
    }

    public ChatLog getChatLogNum(String str){
        ChatLogExample chatLogExample = new ChatLogExample();
        chatLogExample.setOrderByClause("send_time asc");
        ChatLogExample.Criteria criteria = chatLogExample.createCriteria();
        criteria.andReceiveUserEqualTo(str);
        List<ChatLog> list = chatLogService.findAll(chatLogExample);
        return list.get(0);

    }


}
